import React, { Component } from "react";
import "./index.less";
import logo from "../../assets/images/hyt.jpg";
import { Link, withRouter } from "react-router-dom";
import { Menu } from "antd";
import {
  AppstoreOutlined,
  PieChartOutlined,
  DesktopOutlined,
  ContainerOutlined,
} from "@ant-design/icons";
// import menuList from "../../config/menuConfig";
import memoryUtils from "../../utils/memoryUtils";

function getItem(label, key, icon, children, type) {
  return {
    key,
    icon,
    children,
    label,
    type,
  };
}
const items = [
  getItem(<Link to="/home">首页</Link>, "/home", <PieChartOutlined />),
  getItem("商品", "2", <DesktopOutlined />, [
    getItem(
      <Link to="/category">品类管理</Link>,
      "/category",
      <ContainerOutlined />
    ),
    getItem(
      <Link to="/product">商品管理</Link>,
      "/product",
      <ContainerOutlined />
    ),
  ]),
  getItem(<Link to="/user">用户管理</Link>, "/user", <AppstoreOutlined />),
  getItem(<Link to="/role">角色管理</Link>, "/role", <AppstoreOutlined />),
  getItem("图形图案", "7", <AppstoreOutlined />, [
    getItem(<Link to="/bar">柱状图</Link>, "/bar", <ContainerOutlined />),
    getItem(<Link to="/line">折线图</Link>, "/line", <ContainerOutlined />),
    getItem(<Link to="/pie">饼图</Link>, "/pie", <ContainerOutlined />),
  ]),
];

const onClick = (e) => {
  console.log(e.key);
};

// 左侧导航
class LeftNav extends Component {
  render() {
    // 得到当前请求路径
    const psth = this.props.location.pathname;
    items.forEach((item) => {
      if (item.key === psth) {
        memoryUtils.title = item.label.props.children;
        //console.log("1111", memoryUtils.title);
      } else if (("111", item.children)) {
        const cItem = item.children.find((cItem) => cItem.key === psth);
        if (cItem) {
          memoryUtils.title = cItem.label.props.children;
          // console.log("222", memoryUtils.title);
        }
      }
    });
    return (
      <div>
        <div className="left-nav">
          <Link to="/" className="left-nav-header">
            <img src={logo} alt="" />
            <h1>管理后台</h1>
          </Link>
        </div>
        <Menu
          defaultSelectedKeys={[psth]}
          defaultOpenKeys={[psth]}
          mode="inline"
          theme="dark"
          items={items}
          onClick={onClick}
        />
      </div>
    );
  }
}
export default withRouter(LeftNav);
